/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.chassis.customgrid;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JLabel;

import twaver.TWaverConst;
import twaver.network.TNetwork;
import twaver.network.ui.ResizableNodeUI;

public class CustomGridUI extends ResizableNodeUI{
		
	private CustomGrid grid;
	
	private final static JLabel label = new JLabel();
		
	public CustomGridUI(TNetwork network, CustomGrid grid) {
		super(network, grid);
		this.grid = grid;
	}
	
    public void paintBorder(Graphics2D g2d) {
    	if(Boolean.TRUE.equals(network.getClientProperty("Edit Mode"))){
    		super.paintBorder(g2d);
    	}
	}

	public void paintBody(Graphics2D g2d) {
    	Rectangle bounds = grid.getBounds();
        Rectangle innerBounds = grid.getInnerBounds();

    	if(grid.isSelected()){
    		g2d.setColor(new Color(184, 207, 229));
    	}else{
    		g2d.setColor(Color.LIGHT_GRAY);
    	}

        g2d.fillRect(bounds.x, bounds.y, bounds.width-1, bounds.height-1);
        g2d.draw3DRect(bounds.x, bounds.y, bounds.width-1, bounds.height-1, bodyRaised);
        g2d.draw3DRect(innerBounds.x, innerBounds.y, innerBounds.width, innerBounds.height, false);
        
        g2d.setColor(new Color(255, 255, 255, 200));
        g2d.fillRect(innerBounds.x+1, innerBounds.y+1, innerBounds.width-1, innerBounds.height-1);        
        
        g2d.setColor(Color.DARK_GRAY);
        double w = innerBounds.getWidth() / grid.getColumnCount();
        double h = innerBounds.getHeight() / grid.getRowCount();
        
        double x = innerBounds.x + w/2;
        label.setForeground(Color.BLUE);
        for(int i=0; i<grid.getColumnCount(); i++){
        	label.setText(String.valueOf(i));
        	Dimension size = label.getPreferredSize();
        	network.paintComponent(g2d, label, (int)x-size.width/2, innerBounds.y-size.height-2, size.width, size.height);
        	
            g2d.setStroke(TWaverConst.DASHED_STROKE);
        	g2d.drawLine((int)x, (int)(innerBounds.y+h/2), (int)x, (int)(innerBounds.y+innerBounds.height-h/2));
        	x += w;
        }
        
        double y = innerBounds.y + h/2;
        label.setForeground(Color.RED);
        for(int i=0; i<grid.getRowCount(); i++){
        	label.setText(String.valueOf(i));
        	Dimension size = label.getPreferredSize();
        	network.paintComponent(g2d, label, (int)innerBounds.x-size.width-4, (int)y-size.height/2, size.width, size.height);
        	
            g2d.setStroke(TWaverConst.DASHED_STROKE);
        	g2d.drawLine((int)(innerBounds.x+w/2), (int)y, (int)(innerBounds.x+innerBounds.width-w/2), (int)y);
        	y += h;
        }
    }
    
    public boolean contains(int x, int y) {
    	Rectangle bounds = grid.getBounds();
        Rectangle innerBounds = grid.getInnerBounds();
        return bounds.contains(x, y) && !innerBounds.contains(x, y);
    }
}